package controller

import (
	"go_web_todo/pkg/utils"
	"go_web_todo/service"
	"net/http"

	"fmt"

	"github.com/gin-gonic/gin"
)

// @Tags USER
// @Summary 用户注册
// @Produce json
// @Accept json
// @Param data body service.UserService true "用户名, 密码"
// @Success 200 {object} serializer.ResponseUser "{"status":200,"data":{},"msg":"ok"}"
// @Failure 500  {object} serializer.ResponseUser "{"status":500,"data":{},"Msg":{},"Error":"error"}"
// @Router /user/register [post]
func UserRegister(c *gin.Context) {
	fmt.Printf("%v\n", c)
	var userService service.UserService //相当于创建一个对象，直接调用其中的方法
	if err := c.ShouldBindJSON(&userService); err != nil {
		fmt.Printf("参数绑定错误")
		c.JSON(http.StatusBadRequest, ErrorResponse(err))
		utils.Logger().Info(err)
	} else {
		fmt.Printf("username:%v: password: %v \n", userService.UserName, userService.Password)
		res := userService.UserRegisterService()
		c.JSON(http.StatusOK, res)
	}
}

// @Tags USER
// @Summary 用户登录
// @Produce json
// @Accept json
// @Param  data  body  service.UserService  true "user_name, password"
// @Success 200 {object} serializer.ResponseUser "{"success":true,"data":{},"msg":"登陆成功"}"
// @Failure 500 {object} serializer.ResponseUser "{"status":500,"data":{},"Msg":{},"Error":"error"}"
// @Router /user/login [post]
func Login(c *gin.Context) {
	var userService service.UserService
	if err := c.ShouldBindJSON(&userService); err != nil {
		c.JSON(http.StatusBadRequest, ErrorResponse(err))
		utils.Logger().Info(err)
	} else {
		res := userService.Login()
		c.JSON(http.StatusOK, res)
	}
}
